import { HttpClient } from "tsrpc-browser";
import { serviceProto } from "./shared/adminprotocols/serviceProto";
import { enableCookie } from "./models/enableCookie";

class PomeloClient {
    private pomelo: any;
    constructor() {
        this.pomelo = (window as any).pomelo;
    }
    /**
     * 初始化长连接客户端
     * @param params { host: string, port: number, user?: any, handshakeCallback?: any }
     * @param cb  callback
     */
    init(params: { host: string, port: number, user?: any, handshakeCallback?: any ,log?: boolean }, cb: Function) {
        this.pomelo.init(params, cb);
        this.pomelo.on('close', () => {
            console.log('pomelo close');
        });
        this.pomelo.on('error', (err: any) => {
            console.log('pomelo error', err);
        })
        this.pomelo.on('heartbeat timeout', () => {
            console.log('pomelo heartbeat timeout');
        })
        this.pomelo.on('io-error', (err: any) => {
            console.log('pomelo io-error', err);
        })
        this.pomelo.on('onKick', () => {
            console.log('pomelo onKick');
        })
        this.pomelo.on('disconnect',() =>{
            console.log('pomelo disconnect');
        })
    }
    /**
     * 断开连接
     * */
    disconnect() {
        if (this.pomelo) {
            this.pomelo.disconnect();
        }
    }
    /**
     * 发送请求
     * @param route 请求路由
     * @param msg 请求消息
     * @param cb  回调函数
     * */
    request(route: string, msg: any, cb: Function) {
        if (this.pomelo) {
            this.pomelo.request(route, msg, cb);
        }
    }
    /**
     * 发送通知
     * @param route 请求路由
     * @param msg 请求消息
     * */
    notify(route: string, msg: any) {
        if (this.pomelo) {
            this.pomelo.notify(route, msg);
        }
    }
    /**
     * 监听长连接消息
     * @param route 请求路由
     * @param cb  回调函数
     * */
    on(route: string, cb:Function) {
        if (this.pomelo) {
            this.pomelo.on(route, cb);
        }
    }
}
export const pomelo = new PomeloClient();